<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;


use HiCommon\Repository\OrderCheckpointRepository;
use HiCommon\Repository\OrderRepository;
use Modules\SmallApi\Service\PublicService;

class OrderService extends BaseService
{

    /**
     * 添加一条总订单数据
     *
     * @param $s_order_sn
     * @param $n_type
     * @param $n_uid
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_add_one($s_order_sn,$n_type,$n_uid){
        $s_order_sn = empty($s_order_sn) ? '':trim($s_order_sn);
        $n_type = (int)$n_type;
        $n_uid = (int)$n_uid;
        if(empty($s_order_sn)){
            return hello_error('订单号不能为空');
        }
        //订单类型 10检测站年检预约单 11检车站商城单 20维修店服务单 21维修店商城单 30积分商城单 40预约代驾单
        $arr_type = [10,11,20,21,30,40];
        if(!in_array($n_type,$arr_type)){
            return hello_error('订单类型错误');
        }

        $arr_data = [
          'order_sn'=>$s_order_sn,
          'type'=>$n_type,
          'user_id'=>$n_uid,
        ];
        $res = OrderRepository::add_one($arr_data)->toarray();
        if(empty($res)){
            return  hello_error('添加失败');
        }
        return  hello_success('成功');
    }

    /**
     * 用户年检预约单列表
     *
     * @param       $n_uid
     * @param       $arr_search
     * @param array $arr_search_where
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_checkpoint_order_list_by_uid($n_uid,$arr_search,$arr_search_where = []){
        $arr_search_where['user_id'] = $n_uid;
        $arr_option = [
          'where'=>$arr_search_where,
          'field'=>['id','order_sn','main_type','appointment_date','appointment_times','shop_id','user_company_id','price','pay_state','refund_state','appointment_state','complete_state','appraise_state','user_delete','created_at'],
          'with'=>['shop','user_company']
        ];

        $res = OrderCheckpointRepository::base_api_get_list($arr_option)->toarray();
        $data = list_page_data_for_api($res);
        return  hello_success('成功',$data);

    }

    /**
     * 用户-年检预约单详情
     *
     * @param int $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_get_order_checkpoint_detail($n_id= 0){
        $res = OrderCheckpointRepository::api_get_detail($n_id);
        if(is_null($res)){
            return hello_error('订单不存在');
        }
        $arr_data = $res->toarray();

        return  hello_success('成功',$arr_data);

    }

    /**
     * 用户-年检预约单详情
     *
     * @param int $n_uid
     * @param int $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_checkpoint_order_detail_for_user($n_uid = 0,$n_id = 0){
        if($n_id <= 0){
            return hello_error('数据id不能为空');
        }
        $res = self::api_get_order_checkpoint_detail($n_id);
        if($res['state'] != 0){
            return $res;
        }
        $n_user_id = $res['data']['user_id'];

        if($n_user_id != $n_uid){
            return hello_error('非本人订单');
        }
        return  hello_success('成功',$res['data']);

    }

    /**
     * 年检预约单详情
     *
     * @param int $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_checkpoint_order_detail($n_id = 0){
        if($n_id <= 0){
            return hello_error('订单id不能为空');
        }
        $res = OrderCheckpointRepository::api_get_detail($n_id);
        if(is_null($res)){
            return hello_error('订单不存在');
        }

        $arr_data = $res->toarray();

        if($arr_data['user_delete'] == 1){
            return hello_error('订单已删除');
        }
        return  hello_success('成功',$arr_data);

    }

    /**
     * 年检预约单详情
     *
     * @param int $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_order_checkpoint_by_id($n_id = 0){
        if($n_id <= 0){
            return hello_error('数据id不能为空');
        }
        $res = self::api_get_order_checkpoint_detail($n_id);
        if($res['state'] != 0){
            return $res;
        }
        $res = $res['data'];
        foreach ($res['check_vehicle'] as $key=>$value)
        {
            if($value['is_call_helpdriving'] == 1){
                $n_id = intval($value['helpdriving']['id']);
                $result = CommonService::jinchen_helpdriving_order_detail($n_id);
                if($result['state'] == 0){
                    $res['check_vehicle'][$key]['jinchen_helpdriving'] = $result['data'];
                }
            }
            $arrIDs[] = $value;
        }
        return  hello_success('成功',$res);
    }

    /**
     * 用户-年检预约单支付检查
     *
     * @param int $n_uid
     * @param int $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_pay_check_order_checkpoint_for_user($n_uid,$n_id){

        if($n_id <= 0){
            return hello_error('数据id不能为空');
        }
        if($n_uid <= 0){
            return hello_error('用户id不能为空');
        }

        //查询订单信息
        $res = OrderCheckpointRepository::api_get_detail($n_id);
        if(is_null($res)){
            return hello_error('数据不存在');
        }
        $arr_order = $res->toarray();
        $n_user_id = $arr_order['user_id'];
        if($n_user_id != $n_uid){
            return hello_error('非本人订单');
        }

        //判断订单状态
        if($arr_order['pay_state'] == 2){
            return hello_error('订单已支付');
        }

        //订单前置态
        $n_pay_state = $arr_order['pay_state'];
        $n_appointment_state = $arr_order['appointment_state'];
        $real_state = $n_pay_state == 1 && $n_appointment_state == 1;
        if(!$real_state){
            return hello_error('订单状态错误');
        }

        $arr_data = [
          'out_trade_no'=>$arr_order['order_sn'],
          'total_fee'=>$arr_order['price'] * 100// **单位：分**
        ];

        return  hello_success('成功',$arr_data);
    }
}